<?php
namespace app\admin\controller;

use app\admin\model\LogErrorModel;
use thinkless\common\AdminController;
use ui\Button;
use ui\Form;
use ui\FormField;
use ui\MessageBox;
use ui\Table;
use ui\TableColumn;

class Error extends AdminController {

    /**
     * 错误日志页
     */
    public function index() {
        $detailForm = Form::createDialogBuilder('调用栈', ['width' => 920, 'top' => '20px', 'maxHeight' => '70vh'])
            ->addField(FormField::createTextarea('trace', '调用栈', 20));
        $table = Table::create()
            ->withQueryUrl('/admin/error/list')
            ->addButton(Button::createDanger('del_all', MessageBox::createConfirm()->setMethodPost('/admin/error/del')->setContent('确认删除'))->setLabel('删除选中')->setSubmitFilter())
            ->addSelection()
            ->addColumn(TableColumn::create('id', 'ID')->setWidth(60))
            ->addColumn(TableColumn::create('level', '等级')->setWidth(80))
            ->addColumn(TableColumn::create('business_code', '业务码'))
            ->addColumn(TableColumn::create('meta', 'meta'))
            ->addColumn(TableColumn::create('params', 'params'))
            ->addColumn(TableColumn::create('message', 'message')->setMinWidth(180))
            ->addColumn(TableColumn::create('create_time', '创建时间'))
            ->addColumn(TableColumn::create('action', '操作')->setMinWidth(80)
                    ->addButton(Button::create('detail', $detailForm)->setLabel('详情'))
                    ->addButton(Button::createDanger('del', MessageBox::createConfirm()->setMethodPost('/admin/error/del')->setContent('确认删除'))->setLabel('删除'))
                )
            ->buildJson();
        return $this->view('', ['table' => $table]);
    }

    public function list() {
        $list = (new LogErrorModel())->order('id desc')
            ->when(!empty(input('id')), ['id' => input('id')])
            ->when(!empty(input('username')), [['username', 'like', '%'.input('username').'%']])
            ->when(!empty(input('mobile')), [['mobile', 'like', '%'.input('mobile').'%']])
            ->when(!empty(input('status')), ['status' => input('status')])
            ->paginate($this->getPageSize());
        foreach ($list as $item) {
            $item->outputFormat($item);
            $item['actions'] = [
                'detail' => true,
                'del' => true,
            ];
        }
        return $this->jSuccess($list);
    }

    public function del() {
        $ids = input('post.ids');
        return $this->jSuccess($ids);
    }
}